// 获取应用实例
const app = getApp();

Page({
    data: {
        buy_list: [],
        now_page: 0,
        list_total: 0,
        keywords: '',
        limit: 60,
        menu_x: 0,
        menu_y: 0,
        is_show_shade: false,
        is_show_shuaixun: false,
        table_or_card: 1,
        list_order_method: 2,
        expired_status: -1,
        material_type:0,
        showSupply:false,
        showUser:false,
        table_head: [
            {name: '物料', field: 'material_info', is_checked: 0, width: 130},
            {name: '类别', field: 'material_type', is_checked: 1, width: 85},
            {name: '采购数量', field: 'confirm_num', is_checked: 1, width: 140},
            {name: '收料数量', field: 'reviced_num', is_checked: 1, width: 140},
            {name: '未到数', field: 'less_num', is_checked: 1, width: 140},
            {name: '供应商', field: 'supply_info', is_checked: 1, width: 180},
            {name: '成品简码', field: 'template_no', is_checked: 1, width: 130},
            {name: '交期', field: 'dead_line', is_checked: 1, width: 170},
            {name: '逾期天数', field: 'expired_date', is_checked: 1, width: 120},
            {name: '采购时间', field: 'confirm_time', is_checked: 1, width: 170},
            {name: '单价', field: 'price', is_checked: 1, width: 150},
            {name: '到料状态', field: 'reviced_status', is_checked: 1, width: 110},
            {name: '季节', field: 'season', is_checked: 1, width: 100},
            {name: '手工单号', field: 'purchase_sn', is_checked: 1, width: 180},
        ],
        table_scroll_left: 0,
        left_head_style: "",
        table_head_style: "",
        table_content_style: "",
        max_box_style: "",
        content_header_show: true,
        choose_supplier_name: '',
        choose_user_name: '',
        status_checkbox_list: [
            {id: 1, name: '未到', value: 1, is_checked: false},
            {id: 2, name: '部分到', value: 2, is_checked: false},
            {id: 3, name: '到齐', value: 3, is_checked: false},
        ],
        material_type_list: [
            {id: 1, name: '全部', value: 0, is_checked: true},
            {id: 2, name: '面料', value: 1, is_checked: false},
            {id: 3, name: '辅料', value: 2, is_checked: false},
        ],
        overdue_checkbox_list: [
            {id: 1, name: '全部', value: -1, is_checked: true},
            {id: 2, name: '未逾期', value: 1, is_checked: false},
            {id: 3, name: '即将逾期', value: 2, is_checked: false},
            {id: 4, name: '已逾期', value: 3, is_checked: false},
        ],
        supplier_list: [],
        supplier_total: 0,
        supplier_now_page: 0,
        supplier_keywords: '',
        user_list: [],
        user_total: 0,
        user_now_page: 0,
        user_keywords: '',
        search: [
            {field: 'purchase.purchase_sn', val: '', operator: 'LIKE', render: 'tag'},
            {field: 'reviced_status', val: [], operator: 'rangeIn', render: 'tag'},
            {field: 'supply.id', val: -1, operator: '='},
            {field: 'purchase.buyer_id', val: -1, operator: '='},
        ],
        is_show_circular: 1,
        is_allow_request: 1,
    },
    onLoad: function (options) {
        var __this = this;
        // 设置标题
        wx.setNavigationBarTitle({
            title: '采购明细'
        });
        // 设置参数
        if (options != undefined && options.purchase_sn != undefined) {
            this.setData({
                keywords: options.purchase_sn
            });
        }
        // 获取采购明细列表
        this.get_buy_list();
        // 设置筛选球位置
        setTimeout(function () {
            // 获取宽高
            __this.setData({
                menu_x: wx.getSystemInfoSync().windowWidth - 60,
                menu_y: wx.getSystemInfoSync().windowHeight - 70,
            });
        }, 100);
        // 获取供应商
        this.get_supplier_list();
        // 获取供应商
        this.get_user_list();
    },

    // 每次显示时
    onShow() {
        var __this = this;
        if (!app.check_is_login(false, true)) {
            // 显示登录
            wx.showModal({
                title: '提示',
                content: '请登录后再操作',
                showCancel: false,
                complete() {
                    // 获取采购明细列表
                    __this.get_buy_list();
                    // 获取供应商
                    __this.get_supplier_list();
                }
            });
        }
    },

    // 屏幕旋转事件
    onResize:function(res) {
        console.log(res);
        var is_show_circular = 0;
        if (res.deviceOrientation == 'portrait') {
            is_show_circular = 1;
        }
        this.setData({
            is_show_circular: is_show_circular
        })
    },

    // 页面触底时执行
    onReachBottom: function () {
        this.get_buy_list();
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            buy_list: [],
            now_page: 0,
            list_total: 0,
            keywords: '',
        });
        // 获取列表
        this.get_buy_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },
    clickShowSupply:function (e) {
        console.log('点击了')
        this.setData({showSupply:true,showUser:false})
    },
    clickShowUser:function (e) {
        console.log('点击了')
        this.setData({showUser:true,showSupply:false})
    },
    // 监听页面滚动
    onPageScroll: function (e) {

        if (this.data.table_or_card == 2) {
            return false;
        }
        // 如果回到顶部则显示头部搜索
        if (e.scrollTop == 0) {
            this.setData({
                table_head_style: "",
                table_content_style: "",
                left_head_style: "",
                max_box_style: "",
                content_header_show: true
            });
            return false;
        }
        // 如果不在顶部则固定表头并隐藏头部
        if (this.data.content_header_show) {
            this.setData({
                table_head_style: "position: fixed;top: 0;z-index: 82;background:#ffffff;width:calc(100vw - (265vmin / 7.5));",
                left_head_style: "position: fixed;top: 0;z-index: 82;width:calc(200vmin / 7.5);",
                table_content_style: "margin-top: calc(10vmin / 7.5);",
                content_header_show: false,
                max_box_style: "margin-top: calc(70vmin / 7.5);",
            })
        }
    },

    // 当表格滚动时
    tr_boxs_scroll: function (e) {
        // if (app.globalData.platform == 'windows' || app.globalData.platform == 'mac' || app.globalData.platform == 'devtools') {
        //     this.setData({
        //         table_scroll_left: e.detail.scrollLeft
        //     })
        // }
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 获取采购列表
    get_buy_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if ((this.data.buy_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        }
        // 供应商
        if (this.data.search[2].val == -1) {
            this.data.search[2].val = '';
        }
        if (this.data.search[3].val == -1) {
            this.data.search[3].val = '';
        }
        // 组装请求参数
        var req_data = {
            material_type : this.data.material_type,
            page: this.data.now_page + 1,
            quick_search: __this.data.keywords,
            limit: __this.data.limit,
            search: this.data.search,
            expired_status: this.data.expired_status == -1 ? '' : this.data.expired_status
        };
        // 组装排序参数
        if (__this.data.list_order_method == 2) {
            req_data.order = 'order_key,desc|is_finish,asc';
        } else {
            req_data.order = 'purchase_bom_detail_id,desc';
        }
        console.log(req_data)
        // 发起请求
        var result = app.request_api('admin/PurchaseBomDetail/index', req_data, true, 'POST');
        result.then(res => {
            console.log(res)
            // 如果是搜索
            if (is_search == 1) {
                this.setData({
                    buy_list: [],
                    list_total: 0,
                    now_page: 0
                })
            }

            // 重设
            this.setData({
                buy_list: this.data.buy_list.concat(res.list),
                list_total: res.total,
                now_page: this.data.now_page + 1,
                is_allow_request: 1
            });
        });
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            buy_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_buy_list(1);
    },

    // 切换表格或者卡片
    changeTableOrCard: function () {
        var table_or_card = 2;
        if (this.data.table_or_card == 2) {
            table_or_card = 1;
        }
        // 清空数据 重新请求 防止多数据时切换慢
        this.setData({
            table_or_card: table_or_card,
            buy_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_buy_list();
    },

    // 显示筛选
    shuai_xuan_toggle: function () {
        var is_show_shade = true;
        var is_show_shuaixun = true;
        if (this.data.is_show_shuaixun) {
            is_show_shade = false;
            is_show_shuaixun = false;
        }
        this.setData({
            is_show_shade: is_show_shade,
            is_show_shuaixun: is_show_shuaixun,
        });
    },

    // 关闭筛选
    close_shuaixun: function () {
        this.setData({
            is_show_shade: false,
            is_show_shuaixun: false,
            showSupply:false,
            showUser:false
        });
    },

    // 选中状态时
    searchStatusChange: function (e) {
        var __this = this;
        if (e.detail.value.length > 0) {
            this.data.search[1].val = e.detail.value;
            // 将所有标识未选
            __this.data.status_checkbox_list.forEach(function (item, index) {
                __this.data.status_checkbox_list[index].is_checked = false;
            })
            // 将已选中的标识已选
            e.detail.value.forEach(function (item, index) {
                let s_index = __this.data.status_checkbox_list.findIndex(obj => obj.value === parseInt(item));
                if (s_index > -1) {
                    __this.data.status_checkbox_list[s_index].is_checked = true;
                }
            });
        } else {
            this.data.search[1].val = [];
            __this.data.status_checkbox_list.forEach(function (item, index) {
                __this.data.status_checkbox_list[index].is_checked = false;
            })
        }
        this.setData({
            search: this.data.search,
            status_checkbox_list: __this.data.status_checkbox_list
        });
        // 获取数据
        this.get_data_again();
    },

    // 选中是否逾期时
    materialTypeChange: function (e) {
        var s_index = this.data.material_type_list.findIndex(obj => obj.value == parseInt(e.detail.value));
        if (s_index > -1) {
            this.setData({
                material_type: parseInt(e.detail.value)
            });
        }
        // 重新请求数据
        this.get_data_again();
    },

    // 清除供应商
    clearSupplier: function (events) {
        this.data.search[2].val = -1;
        this.setData({
            search: this.data.search,
            choose_supplier_name: ''
        });
        // 获取数据
        this.get_data_again();
    },

    // 清除供应商
    clearUser: function (events) {
        this.data.search[3].val = -1;
        this.setData({
            search: this.data.search,
            choose_user_name: ''
        });
        // 获取数据
        this.get_data_again();
    },

    // 获取供应商
    get_supplier_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if ((this.data.supplier_list.length >= this.data.supplier_total) && this.data.supplier_now_page > 0) {
            return false;
        }
        // 发起请求
        var result = app.request_api('admin/Supply/index', {
            page: this.data.supplier_now_page + 1,
            quick_search: __this.data.supplier_keywords,
            limit: __this.data.limit,
            initKey: 'id',
            initValue: '',
            select: true
        });
        result.then(res => {
            if (is_search == 1) {
                this.setData({
                    supplier_list: [],
                    supplier_now_page: 0,
                    supplier_total: 0
                })
            }
            __this.setData({
                supplier_list: __this.data.supplier_list.concat(res.list),
                supplier_now_page: this.data.supplier_now_page + 1,
                supplier_total: res.total
            })
        });
    },

    // 获取供应商
    get_user_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if ((this.data.user_list.length >= this.data.user_total) && this.data.user_now_page > 0) {
            return false;
        }
        // 发起请求
        var result = app.request_api('admin/auth.admin/index', {
            page: this.data.user_now_page + 1,
            quick_search: __this.data.user_keywords,
            limit: __this.data.limit,
            initKey: 'id',
            initValue: '',
            select: true
        });
        result.then(res => {
            if (is_search == 1) {
                this.setData({
                    user_list: [],
                    user_now_page: 0,
                    user_total: 0
                })
            }
            __this.setData({
                user_list: __this.data.user_list.concat(res.list),
                user_now_page: this.data.user_now_page + 1,
                user_total: res.total
            })
        });
    },


    // 选择完供应商时
    userChange: function (e) {
        console.log(e)
        var user_supplier_name = '';
        var index = this.data.user_list.findIndex(obj => obj.id === parseInt(e.detail.value));
        if (index > -1) {
            user_supplier_name = this.data.user_list[index].realname;
        }
        this.data.search[3].val = e.detail.value;
        this.setData({
            search: this.data.search,
            user_supplier_name: user_supplier_name
        });
        // 获取数据
        this.get_data_again();
        // this.setData({showSupply:false})
    },
    // 选择完供应商时
    supplierChange: function (e) {
        console.log(e)
        var choose_supplier_name = '';
        var index = this.data.supplier_list.findIndex(obj => obj.id === parseInt(e.detail.value));
        if (index > -1) {
            choose_supplier_name = this.data.supplier_list[index].SupplyNa;
        }
        this.data.search[2].val = e.detail.value;
        this.setData({
            search: this.data.search,
            choose_supplier_name: choose_supplier_name
        });
        // 获取数据
        this.get_data_again();
        // this.setData({showSupply:false})
    },

    // 输入手工单时
    searchInputChange: function (events) {
        console.log(events)
        this.data.search[0].val = events.detail.value.trim();
        this.setData({
            search: this.data.search
        })
        console.log(this.data.search)
        // 获取数据
        this.get_data_again(1);
    },

    // 重新请求数据
    get_data_again: function (is_search = 0) {
        // 重设data
        this.setData({
            buy_list: [],
            now_page: 0,
            list_total: 0,
        });
        // 重新调用数据
        this.get_buy_list(is_search);
    },

    // 重新请求数据
    get_user_again: function (is_search = 0) {
        // 重设data
        this.setData({
            buy_list: [],
            now_page: 0,
            list_total: 0,
        });
        // 重新调用数据
        this.get_buy_list(is_search);
    },

    // 搜索供应商时
    searchSupplierChange: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            supplier_keywords: keywords,
            supplier_list: [],
            supplier_now_page: 0,
            supplier_total: 0,
        });
        this.get_supplier_list(1);
    },


    // 搜索供应商时
    searchUserChange: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            user_keywords: keywords,
            user_list: [],
            user_now_page: 0,
            user_total: 0,
        });
        this.get_user_list(1);
    },

    // 选中是否逾期时
    searchOverdueChange: function (e) {
        var s_index = this.data.overdue_checkbox_list.findIndex(obj => obj.value == parseInt(e.detail.value));
        if (s_index > -1) {
            this.setData({
                expired_status: parseInt(e.detail.value)
            });
        }
        // 重新请求数据
        this.get_data_again();
    },

    // 改变排序状态
    changeListOrder: function () {
        var list_order_method = 1;
        if (this.data.list_order_method == 1) {
            list_order_method = 2;
        }
        this.setData({
            list_order_method: list_order_method
        });
        // 重新请求数据
        this.get_data_again();
    },

    // 显示全名
    showMore: function (events) {
        var index = events.currentTarget.dataset.index;
        var content = this.data.buy_list[index].material_info;
        wx.showToast({
            title: content,
            icon: 'none',
            duration: 3000,
        });
    },

    // 开关事件
    switchFinishChange: function (e) {
        console.log(e);
        var __this = this;
        var index = e.currentTarget.dataset.index;
        // 先阻止其改变状态
        // if (e.detail.value) {
        //     __this.data.buy_list[index].is_finish = 0;
        // } else {
        //     __this.data.buy_list[index].is_finish = 1;
        // }
        // this.setData({
        //     buy_list: __this.data.buy_list
        // });
        // 发起请求
        var result = app.request_api('admin/purchaseBomDetail/signFinish', {
            purchase_bom_detail_id: __this.data.buy_list[index].purchase_bom_detail_id
        }, true);
        result.then(res => {
            console.log(res);
            __this.data.buy_list[index].is_finish = e.detail.value ? 1 : 0;
            this.setData({
                buy_list: __this.data.buy_list
            });
        });
    },


})
